import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/base_widget/image_network_widget.dart';
import 'package:jetour_line/base_widget/loading_widget.dart';
import 'package:jetour_line/base_widget/my_refresh_view.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/network/aha_http.dart';
import 'package:jetour_line/network/jetour_http.dart';
import 'package:jetour_line/pages/common/commonDetail/common_detail_page.dart';
import 'package:jetour_line/pages/creatTravel/creat_travel_page.dart';
import 'package:jetour_line/pages/mine/trajectoryDetail/trajectory_detail_page.dart';
import 'package:jetour_line/utils/extension_int.dart';
import 'package:jetour_line/utils/page_push_utils.dart';

import '../../base_widget/my_alert.dart';
import '../../model/trajectory_lsit_model.dart';

class MineTrajectoryPageScreen extends StatefulWidget {
  const MineTrajectoryPageScreen({Key? key}) : super(key: key);

  @override
  State<MineTrajectoryPageScreen> createState() => _MineTrajectoryPageScreenState();
}

class _MineTrajectoryPageScreenState extends State<MineTrajectoryPageScreen> {
  List<ListElement>? list;
  int page = 1;
  final MyRefreshViewController _controller = MyRefreshViewController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getList();
  }

  @override
  Widget build(BuildContext context) {

    return BaseScaffold(
        backgroundColor: MyColors.colorF1F1F1,
        title: "我的轨迹",
        body: (list == null ||list!.isEmpty)?LoadingWidget(type:(list != null && list!.isEmpty)?1: 0,): Padding(
          padding: EdgeInsets.all(20.px),
          child: MyRefreshView(
            refreshCallBack: (bool isUp) {
              if (isUp) {
                page = 1;
              } else {
                page++;
              }
              getList();
            },
            controller: _controller,
            child: ListView.builder(
                itemCount: list!.length,
                itemBuilder: (context, index) {
                  return itemWidget(index);
                }),
          ),
        ));
  }

  Widget itemWidget(int index) {
    String caption = list![index].caption;
    String time = '于${list![index].time.year}-${list![index].time.month}-${list![index].time.day}创建';



    String dis = (list![index].distance<1000)? '总里程${list![index].distance}m':'总里程${(list![index].distance/1000).toStringAsFixed(2)}KM';

    if(list![index].distance>1000){
      double disD = list![index].distance/1000;
      dis = '总里程${disD.toStringAsFixed(2)}km';
    }else{
      dis = '总里程${list![index].distance}m';
    }
    return Padding(
      padding: EdgeInsets.only(
        left: 10.px,
        top: 20.px,
        right: 10.px,
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(16.px)),
        child: GestureDetector(
          onTap: () {
            PagePushUtils.push(context, TrajectoryDetailScreen(id: list![index].id));
          },
          child: Container(
            color: MyColors.white,
            width: double.infinity,
            child: Padding(
              padding: EdgeInsets.all(
                24.px,
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    caption,
                    style: TextStyle(
                        fontSize: 30.px, color: MyColors.color333333),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 15.px,),
                  Row(children: [
                    Text(
                      time,
                      style: TextStyle(
                          fontSize: 28.px, color: MyColors.color666666),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    SizedBox(width: 10.px,),
                    Expanded(child: Text(
                      dis,
                      style: TextStyle(
                          fontSize: 26.px, color: MyColors.color999999),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),),
                    GestureDetector(
                      onTap: () {
                        delCommon(index);
                      },
                      child: Container(
                        //设置 child 居中
                        alignment: Alignment.center,
                        height: 50.px,
                        width: 130.px,
                        //边框设置
                        decoration: BoxDecoration(
                          //背景
                          color: Colors.white,
                          //设置四周圆角 角度
                          borderRadius:
                          BorderRadius.all(Radius.circular(4.px)),
                          //设置四周边框
                          border: Border.all(
                              width: 1, color: MyColors.colorred),
                        ),
                        child: Text(
                          "删除",
                          style: TextStyle(
                              fontSize: 26.px,
                              color: MyColors.colorred),
                        ),
                      ),
                    ),
                  ],),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  void getList() {
    Map<String, dynamic> params = Map();
    params['page'] = page;
    AhaHttp().get(AhaRouter.MineTrankList, params: params).then((value) {
      TrajectoryListModel listModel = TrajectoryListModel.fromJson(value);
      list ??= [];
      if(page == 1){
        list?.clear();
      }
      list?.addAll(listModel.list);
      _controller.refreshEnd(page: page,maxPage: listModel.meta.maxPage);
      setState(() {});

    });
  }

  void delCommon(int item) {
    showMyDialog(context, '是否删除', "是", "否", (index) {
      if(index == 0){
        Map<String, dynamic> params = Map();
        params['id'] = list![item].id;
        AhaHttp().post(AhaRouter.MineTrankDelete, data: params).then((value) {
          print(value);
          list!.removeAt(item);
          setState(() {});
          if (list!.isEmpty) {
            _controller.beginRef();
          }
        });
      }

    });

  }
}
